# -*- mode: Makefile -*-
#
# Copyright (C) 2013-2016 DNAnexus, Inc.
#
# This file is part of dx-toolkit (DNAnexus platform client libraries).
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may not
#   use this file except in compliance with the License. You may obtain a copy
#   of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

# curl_build_dir := $(shell mktemp -d --tmpdir=/tmp curl-build-XXXXXX)

# curl:
# 	$(DNANEXUS_HOME)/src/dx-verify-file/build_curl.sh $(curl_build_dir)

DXTOOLKIT_GITVERSION := $(shell git describe|sed 's/-/+/')
VERSION = 0.0.1

curl_dir = $(HOME)/sw/local
cpp_dir = $(DNANEXUS_HOME)/src/cpp
dxjson_dir = $(cpp_dir)/dxjson
dxhttp_dir = $(cpp_dir)/SimpleHttpLib
dxcpp_dir = $(cpp_dir)/dxcpp
dx-verify-file_dir = $(DNANEXUS_HOME)/src/dx-verify-file

VPATH = $(dxjson_dir):$(dxhttp_dir):$(dxcpp_dir):$(dx-verify-file_dir)

CFLAGS = -g -Wall -Wextra

UNAME := $(shell uname)

ifeq ($(UNAME), Linux)
  CXXFLAGS = -DSTATIC_BUILD -DLINUX_BUILD -D_FILE_OFFSET_BITS=64 -DDX_VERIFY_FILE_VERSION=\"$(VERSION)\" -DDXTOOLKIT_GITVERSION=\"$(DXTOOLKIT_GITVERSION)\" -g -Wall -Wextra -Werror=return-type -std=c++0x -I$(curl_dir)/include -I$(cpp_dir) -I$(dxhttp_dir) -I$(dxjson_dir) -I$(dxcpp_dir) -I$(dx-verify-file_dir)
  LDFLAGS := -static -pthread -L/usr/lib -L$(curl_dir)/lib -lcurl -lcares -lssl -lcrypto -lrt -ldl -lz -lboost_program_options -lboost_filesystem -lboost_system -lboost_thread-mt -lboost_regex
else ifeq ($(UNAME), Darwin)
  CXXFLAGS = -DSTATIC_BUILD -DMAC_BUILD -D_FILE_OFFSET_BITS=64 -DDX_VERIFY_FILE_VERSION=\"$(VERSION)\" -DDXTOOLKIT_GITVERSION=\"$(DXTOOLKIT_GITVERSION)\" -g -Wall -Wextra -Werror=return-type -std=c++0x -I$(curl_dir)/include -I$(cpp_dir) -I$(dxhttp_dir) -I$(dxjson_dir) -I$(dxcpp_dir) -I$(dx-verify-file_dir)
  boost_ldir_mac = /opt/local/lib
  LDFLAGS := $(boost_ldir_mac)/libboost_program_options-mt.a $(boost_ldir_mac)/libboost_thread-mt.a $(boost_ldir_mac)/libboost_filesystem-mt.a $(boost_ldir_mac)/libboost_regex-mt.a $(curl_dir)/lib/libcurl.a /opt/local/lib/libssl.a /opt/local/lib/libcrypto.a /opt/local/lib/libcares.a /opt/local/lib/libz.a $(boost_ldir_mac)/libboost_system-mt.a
else
  $(error No LDFLAGS for system $(UNAME))
endif

dxjson_objs = dxjson.o
dxhttp_objs = SimpleHttp.o SimpleHttpHeaders.o Utility.o
dxcpp_objs = api.o dxcpp.o SSLThreads.o utils.o dxlog.o
dx-verify-file_objs = options.o log.o chunk.o main.o File.o

dxjson: $(dxjson_objs)
dxhttp: $(dxhttp_objs)
dxcpp: $(dxcpp_objs)
dx-verify-file: $(dx-verify-file_objs)

all: dxjson dxhttp dxcpp dx-verify-file
	g++ *.o $(LDFLAGS) -o dx-verify-file
ifeq ($(UNAME), Darwin)
	cp $(dx-verify-file_dir)/ca-certificates.crt  .
	cp -v /opt/local/lib/gcc47/libstdc++.6.dylib /opt/local/lib/gcc47/libgcc_s.1.dylib .
	install_name_tool -change /opt/local/lib/libstdc++.6.dylib @executable_path/libstdc++.6.dylib dx-verify-file
	install_name_tool -change /opt/local/lib/gcc47/libgcc_s.1.dylib @executable_path/libgcc_s.1.dylib dx-verify-file
	install_name_tool -change /opt/local/lib/gcc47/libgcc_s.1.dylib @executable_path/libgcc_s.1.dylib libstdc++.6.dylib
endif

dist: all
ifeq ($(UNAME), Linux)
	mv -v dx-verify-file dx-verify-file-$(VERSION)-linux
	bzip2 -9 dx-verify-file-$(VERSION)-linux
else ifeq ($(UNAME), Darwin)
	mkdir -v dx-verify-file-$(VERSION)-mac
	mv -v dx-verify-file ca-certificates.crt libstdc++.6.dylib libgcc_s.1.dylib dx-verify-file-$(VERSION)-mac
	tar jcvf dx-verify-file-$(VERSION)-mac.tar.bz2 dx-verify-file-$(VERSION)-mac
else
	$(error No dist recipe for system $(UNAME))
endif

clean:
	rm -v *.o dx-verify-file

.PHONY: all dxjson dxhttp dxcpp dx-verify-file
